<template>
  <div class="account-box">
    <div class="toggle">
      <a href="javascript:;" v-if="activeForm==='message'" @click="activeForm='account'">
        <i class="iconfont icon-user"></i> 使用账号登录
      </a>
      <a href="javascript:;" v-if="activeForm==='account'" @click="activeForm='message'">
        <i class="iconfont icon-msg"></i> 使用短信登录
      </a>
    </div>
    <Form class="form" ref="formCom" :validation-schema="veeValidateSchema" v-slot="{ errors }">
        <h3>打印错误对象{{errors}}</h3>
      <template v-if="activeForm==='account'">
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-user"></i>
            <Field name="account" :class="{error:errors.account}" v-model="form.account" type="text" placeholder="请输入用户名或手机号" />
          </div>
          <div class="error" v-if="errors.account"><i class="iconfont icon-warning" />{{errors.account}}</div>
        </div>
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-lock"></i>
            <Field name="password" :class="{error:errors.password}" v-model="form.password" type="password" placeholder="请输入密码"/>
          </div>
          <div class="error" v-if="errors.password"><i class="iconfont icon-warning" />{{errors.password}}</div>
        </div>
      </template>
      <template v-else>
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-user"></i>
            <Field name="mobile" :class="{error:errors.mobile}" v-model="form.mobile" type="text" placeholder="请输入手机号" />
          </div>
          <div class="error" v-if="errors.mobile"><i class="iconfont icon-warning" />{{errors.mobile}}</div>
        </div>
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-code"></i>
            <Field name="code" :class="{error:errors.code}" v-model="form.code" type="text" placeholder="请输入验证码"/>
            <span class="code" @click="sendCodeBtn">{{number===0?'发送验证码':number+'秒后发送'}}</span>
          </div>
          <div class="error" v-if="errors.code"><i class="iconfont icon-warning" />{{errors.code}}</div>
        </div>
      </template>
      <div class="form-item">
        <div class="agree">
          <!-- <XtxCheckbox /> -->
          <!-- 💥 Field 可以通过 as 属性指定渲染成其他标签或者自定义组件，as 默认值为 input -->
          <Field as="XtxCheckbox" v-model="form.isAgree" name="isAgree" />
          <span>我已同意</span>
          <a href="javascript:;">《隐私条款》</a>
          <span>和</span>
          <a href="javascript:;">《服务条款》</a>
        </div>
        <div class="error" v-if="errors.isAgree"><i class="iconfont icon-warning" />{{errors.isAgree}}</div>
      </div>
      <a href="javascript:;" class="btn" @click="loginBtn">登录</a>
    </Form>
    <div class="action">
      <a href="https://graph.qq.com/oauth2.0/show?which=Login&display=pc&client_id=100556005&response_type=token&scope=all&redirect_uri=http%3A%2F%2Fwww.corho.com%3A8080%2F%23%2Flogin%2Fcallback">
          <img src="https://qzonestyle.gtimg.cn/qzone/vas/opensns/res/img/Connect_logo_7.png" alt="">
      </a>
      <div class="url">
        <a href="javascript:;">忘记密码</a>
        <a href="javascript:;">免费注册</a>
      </div>
    </div>
  </div>
</template>

<script>
import { reactive, ref } from 'vue'
import { Field, Form } from 'vee-validate'
import veeValidateSchema from '@/utils/vee-validate-schema'
import { userAccountLogin, userMobileLogin, userMobileLoginMsg } from '@/api/user'
import { login, sendCode } from '@/utils/formbtn'
export default {
  name: 'LoginForm',
  components: {
    Field, Form
  },
  setup () {
    //   切换账号登录和短信登录
    const activeForm = ref('message')
    // 收集表单数据
    const form = reactive({
      account: null,
      password: null,
      mobile: null,
      code: null,
      isAgree: true
    })
    // 定义表单的ref
    const formCom = ref(null)
    // 登录按钮点击事件
    const loginBtn = async () => {
      // 如果在账号登录界面，使用账号登录接口
      if (activeForm.value === 'account') {
        login(userAccountLogin, form, formCom)
      } else {
        // 否则使用短信登录接口
        login(userMobileLogin, form, formCom)
      }
    }
    const number = ref(0)
    const sendCodeBtn = () => {
      // 发送验证码封装的函数
      sendCode(userMobileLoginMsg, form.mobile, formCom, number)
    }
    return { activeForm, form, loginBtn, formCom, veeValidateSchema, sendCodeBtn, number }
  }
}
</script>

<style lang="less" scoped>
// 账号容器
.account-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;
    a {
      color: @xtxColor;
      i {
        font-size: 14px;
      }
    }
  }
  .form {
    padding: 0 40px;
    &-item {
      margin-bottom: 28px;
      .input {
        position: relative;
        height: 36px;
        > i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }
        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;
          &.error {
            border-color: @priceColor;
          }
          &.active,&:focus {
            border-color: @xtxColor;
          }
        }
        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }
      > .error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        color: @priceColor;
        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }
    .agree {
      a {
        color: #069;
      }
    }
    .btn {
      display: block;
      width: 100%;
      height: 40px;
      color: #fff;
      text-align: center;
      line-height: 40px;
      background: @xtxColor;
      &.disabled {
        background: #cfcdcd;
      }
    }
  }
  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}
</style>
